﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class StartView : MonoBehaviour
{
    public UnityAction submit = null;
    public UnityAction<float> timeSubmit = null;
    public UnityAction<float> Gameover = null;
    public Button beginBtn;
    public Text scoreText;
    public Text timeText;
    public Text lastText; //结束Text
    public bool flag = false;
    // Use this for initialization
    void Start()
    {
        beginBtn = transform.Find("BeginButton").GetComponent<Button>();
        scoreText = transform.Find("ScoreImage/Score").GetComponent<Text>();
        timeText = transform.Find("ScoreImage/Time").GetComponent<Text>();
        lastText = transform.Find("ScoreImage/Game").GetComponent<Text>();
        //游戏开始
        beginBtn.onClick.AddListener(() =>
        {
            if(submit!=null)
            {
                submit();
            }
            flag = true;
            StartCoroutine(GameBegin());
        });
    }
    /// <summary>
    /// 更新V面板
    /// </summary>
    /// <param name="score"></param>
    /// <param name="times"></param>
    public void UpdateView(int score, float times)
    {
        scoreText.text = score.ToString();
        timeText.text = times.ToString();
    }
    /// <summary>
    /// 游戏结束
    /// </summary>
    public void GameOver(int score)
    {
        Debug.Log("游戏结束");
        if (score >= 7)
        {
            lastText.text = "游戏胜利";
        }
        else
        {
            lastText.text = "游戏失败";
        }
        flag = false;
    }
    /// <summary>
    /// 游戏开始
    /// </summary>
    /// <returns></returns>
    IEnumerator GameBegin()
    {
        WaitForSeconds a = new WaitForSeconds(1);
        if (flag)
        {
            for (int i = 0; i < 10; i++)
            {
                yield return a;
                if (timeSubmit != null)
                {
                    timeSubmit(float.Parse(timeText.text));
                }
            }
            if (Gameover != null)
            {
                Gameover(float.Parse(timeText.text));
            }
        }

    }
}
